<?php

/**
 * Member controller
 */
class MemberController extends Controller {
	/**
	 * Declares class-based actions.
	 */
	public function actions(){
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'offset' => 2,
				'transparent' => true,
			),
		);
	}
	public function filters(){
        return array(
            'accessControl',
        );
    }
	public function accessRules(){
        return array(
 			
        );
    }
    public function actionIndex(){
    	var_dump(Yii::app()->user->checkAccess("admin"));die;
    }

    public function actionSignup(){
    	if(!empty($_GET['member'])){
    		if($_GET['member'] == "customer"){
    			$this->forward('signupCustomer');
    		}else if($_GET['member'] == "company"){
    			$this->forward('signupCompany');
    		}

    	}
    	$this->render("signup");
    }

    public function actionSignupCustomer(){
    	$model = new SignupCustomerForm;
    	$this->performAjaxValidation($model);
    	if(Yii::app()->request->isPostRequest){
    		$model->setAttributes($_POST['SignupCustomerForm'],false);
    		if($model->validate()){
    			$model->signup();
			}
    	}
    	$this->render("signup_customer",array(
    		'model' => $model,
		));
    }

    public function actionSignupCompany(){
    	$model = new SignupCompanyForm();
    	$this->performAjaxValidation($model);
    	if(Yii::app()->request->isPostRequest){
    		$model->setAttributes($_POST['SignupCompanyForm'],false);
    		if($model->validate()){
    			$model->signup();
			}
    	}
    	$this->render("signup_company",array(
    		'model' => $model,
		));
    }

    public function actionSignin(){
		$model = new loginForm();
		if(Yii::app()->request->isPostRequest){
			if(!empty($_POST['loginForm'])){
				$model->setAttributes(@$_POST['loginForm'],false);
			}
			if($model->validate()){
				$result = $model->login();
				if($result === true) {
					$this->redirect(Yii::app()->params['homeUrl']);
				}
			}
		}
    	$this->render("signin",array(
    		'model_login_n' => $model
		));
    }

	public function performAjaxValidation($model){
		if(Yii::app()->getRequest()->getIsAjaxRequest()) {
			echo CActiveForm::validate( array( $model)); 
			Yii::app()->end(); 
		}
	}

	// -----------------------------------------------------------
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}